package com.qyc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qyc.dto.AddOrUpdateIndexRecordDTO;
import com.qyc.enums.CodeMsgEnum;
import com.qyc.exception.SystemException;
import com.qyc.pojo.IndexRecord;
import com.qyc.service.IndexRecordService;
import com.qyc.mapper.IndexRecordMapper;
import com.qyc.utils.BeanCopyUtils;
import com.qyc.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author qinyc
* @description 针对表【index_record(动物指标记录表)】的数据库操作Service实现
* @createDate 2023-06-25 21:00:24
*/
@Service
public class IndexRecordServiceImpl extends ServiceImpl<IndexRecordMapper, IndexRecord>
    implements IndexRecordService{

    //注入指标记录Mapper层对象
    @Autowired
    private IndexRecordMapper indexRecordMapper;

    /**
     * @Author qinyc
     * @Description  分页多条件查询指标记录
     * @version: v1.0
     * @Date 23:06 2023/6/25
     **/
    @Override
    public ResponseResult queryPage(Integer pageNum, Integer pageSize, Integer startWeight, Integer endWeight) {
        //创建条件选择器
        LambdaQueryWrapper<IndexRecord> indexRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询体重区间的数据
        //输入最小值，大于等于最小值（ge方法：大于等于  gt方法：大于）
        indexRecordLambdaQueryWrapper.ge(Objects.nonNull(startWeight),IndexRecord::getIrdWeight,startWeight);
        //输入最大值，小于等于最大值（le方法：小于等于  lt方法：小于）
        indexRecordLambdaQueryWrapper.le(Objects.nonNull(endWeight),IndexRecord::getIrdWeight,endWeight);
        //开启PageHelper插件
        PageHelper.startPage(pageNum,pageSize);
        //核心查询
        List<IndexRecord> indexRecords = indexRecordMapper.selectList(indexRecordLambdaQueryWrapper);
        //创建PageInfo对象，将查询到的结果封装在PageInfo对象中
        PageInfo pageInfo = new PageInfo(indexRecords);
        //返回统一响应信息
        return ResponseResult.success(pageInfo);
    }

    /**
     * @Author qinyc
     * @Description  新增或修改指标
     * @version: v1.0
     * @Date 10:57 2023/6/26
     **/
    @Override
    public ResponseResult saveOrUpdate(AddOrUpdateIndexRecordDTO addOrUpdateIndexRecordDTO) {
        //参数校验
        //参数校验完毕
        //bean拷贝
        IndexRecord indexRecord = BeanCopyUtils.copyBean(addOrUpdateIndexRecordDTO, IndexRecord.class);
        /*
         * 判断数据传输对象中是否含有指标ID
         */
        if (Objects.nonNull(addOrUpdateIndexRecordDTO.getIrdId())){
            //指标ID不为空  修改
            indexRecordMapper.updateById(indexRecord);
            //返回统一响应
            return ResponseResult.success();
        }else{
            //指标ID为空  新增
            indexRecordMapper.insert(indexRecord);
            //返回统一响应
            return ResponseResult.success();
        }
    }

    /**
     * @Author qinyc
     * @Description  根据id删除指标记录
     * @version: v1.0
     * @Date 11:23 2023/6/26
     **/
    @Override
    public ResponseResult deleteById(Integer irdId) {
        //参数校验
        if (Objects.isNull(irdId)){
            //指标记录ID不能为空
            throw new SystemException(CodeMsgEnum.INDEX_RECORD_ID_IS_NOT_NULL);
        }else {
            //根据指标记录ID查询指标记录
            IndexRecord indexRecord = indexRecordMapper.selectById(irdId);
            if (Objects.isNull(indexRecord)){
                //指定指标记录不存在
                throw new SystemException(CodeMsgEnum.INDEX_RECORD_IS_NOT_EXIST);
            }
            //指定指标记录存在
            //根据指标记录ID删除指定指标记录
            indexRecordMapper.deleteById(irdId);
            //返回统一响应
            return ResponseResult.success();
        }
    }

    /**
     * @Author qinyc
     * @Description  批量删除指标记录
     * @version: v1.0
     * @Date 11:38 2023/6/26
     **/
    @Override
    public ResponseResult deleteByIdAll(List<Integer> irdIds) {
        /*
         * 参数校验
         */
        //参数校验完毕
        //根据ID集合实现批量删除
        indexRecordMapper.deleteBatchIds(irdIds);
        //返回统一响应
        return ResponseResult.success();
    }
}




